package lib;

import lib.gui.ScreenTemplate;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class Icon extends VerticalFieldManager implements FocusChangeListener
{
	private Bitmap icon; // Icons are to be 80 / 80
	private String label;
	private BitmapField btnIcon;
	private LabelField title;
	private NullField dummy;
	private static final Background focusBackground = BackgroundFactory.createSolidTransparentBackground(Color.DEEPSKYBLUE, 50);
	private static final Background defaultBackground = BackgroundFactory.createSolidTransparentBackground(Color.ANTIQUEWHITE, 0);
	public static final int IconTitleColor = Color.WHITE;
	public Icon(String label, Bitmap icon)
	{
		super(Field.FIELD_HCENTER | Field.FIELD_VCENTER | DrawStyle.HCENTER);
		this.setIcon(icon);
		btnIcon = new BitmapField(icon);
		this.setLabel(label);
		title = new LabelField(label, Field.FIELD_HCENTER | DrawStyle.HCENTER)
		{
			public void paint(Graphics g)
		    {
		        g.setColor(IconTitleColor);		        
		        super.paint(g);
		    } 
		};
		title.setFont(ScreenTemplate.valerieHandFont.getFont(Font.BOLD, 20));
		
		this.setBackground(Icon.defaultBackground);
		
		this.add(btnIcon);
		this.add(title);
		this.setPadding(new XYEdges(5,5,5,5));
		
		dummy = new NullField(Field.FOCUSABLE)
		{};
		dummy.setFocusListener(this);
		this.add(dummy);
		this.setBorder(BorderFactory.createRoundedBorder(new XYEdges(4,4,4,4), Color.WHITE, Border.STYLE_TRANSPARENT));
	}
	public void setDummy(NullField dummy) {
		this.dummy = dummy;
	}
	public void focusChanged(Field field, int eventType) 
	{
		if(dummy.isFocus())
		{
			this.setBackground(Icon.focusBackground);
			
		}else
		{
			this.setBackground(Icon.defaultBackground);
		}
		
	}
	public void setIcon(Bitmap icon) {
		this.icon = icon;
	}
	public Bitmap getIcon() {
		return icon;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getLabel() {
		return label;
	}
	
	
	public int getPreferredWidth() {
		   return Math.max(btnIcon.getWidth()+5, title.getWidth()+5);
		}
		   
	public int getPreferredHeight(){
		return _height;
	}
	public void setHeight(int h){
		_height = h;
	}
	private int _height;
	
	public boolean isFocus()
	{
		return this.dummy.isFocus();
	}

}
